/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.infosystem.noeplaza.lms.struts.action;

import com.eit.toschoolonline.lms.constant.LMSConstant;
import com.eit.toschoolonline.lms.frontend.jdo.LearningItemBookmarkM;
import com.eit.toschoolonline.lms.frontend.jdo.LearningItemViewM;
import com.eit.toschoolonline.lms.frontend.service.LmsViewService;
import com.eit.toschoolonline.lms.frontend.jdo.LearningItemHistoryM;
//import com.eit.toschoolonline.lms.jdo.LearningItemM;
//import com.eit.toschoolonline.lms.service.LearningItemHistoryService;
//import com.eit.toschoolonline.lms.service.LmsServiceFactory;
import com.eit.toschoolonline.lms.frontend.service.LearningItemHistoryService;
import com.eit.toschoolonline.openid.jdo.OpenIdUserM;
import com.eit.toschoolonline.openid.service.spring.OpenIdService;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;

/**
 *
 * @author tawatchai
 */
public class EitemViewAction extends Action {

    public static String ITEM_ATTR = "ITEM_VIEW_ATTR";
    public static String ITEM_BOOKMARK_ATTR = "ITEM_BOOKMARK_ATTR";
//    public static String ITEM_ITEMPATH = "ITEM_VIEW_ITEMPATH";
//    public static String ITEM_FILENAME = "ITEM_VIEW_FILENAME";
//    private LmsServiceFactory lmsServiceFactory;

    private LmsViewService lmsViewService;
     private LearningItemHistoryService historyService;
     private OpenIdService openIdService;

    private Map bgColorMap;

    public Map getBgColorMap() {
        return bgColorMap;
    }

    public void setBgColorMap(Map bgColorMap) {
        this.bgColorMap = bgColorMap;
    }

//    public LmsServiceFactory getLmsServiceFactory() {
//        return lmsServiceFactory;
//    }
//
//    public void setLmsServiceFactory(LmsServiceFactory lmsServiceFactory) {
//        this.lmsServiceFactory = lmsServiceFactory;
//    }
    //@Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String forwardPage = "error";
        String refId = request.getParameter("refId");
        String sessionId = request.getSession().getId();
//        String username = request.getRemoteUser();
//         OpenIdUserM openIdUser = (OpenIdUserM) request.getSession().getAttribute("OpenId");
//        if (openIdUser==null){
//            openIdUser = openIdService.getOpenIdUser(username);
//            if (openIdUser==null){
//                System.out.println("not found - no username");
//                return mapping.findForward("notFoundHistory");
//            };
//            request.getSession().setAttribute("OpenId",openIdUser);
//
//        };
                
//        LearningItemHistoryService hService = lmsServiceFactory.getLearningItemHistoryService();
         LearningItemHistoryM itemHistory = historyService.getFullLearningItemHistory(refId,sessionId);
         if (itemHistory==null){
             Integer itemCode = historyService.getItemCode(refId);
             if (itemCode!=null){
                 String targetAction = "/showItem.do";
                 ActionRedirect ar = new ActionRedirect(targetAction);
                //ar.addParameter("path", new Integer(item.getItemPath()));
                ar.addParameter("id", itemCode.intValue());
             }
             return mapping.findForward("notFoundHistory");
         }


        // int itemCode = itemHistory.getLearningCode();

         LearningItemViewM item =  lmsViewService.getItemView(itemHistory.getLearningCode());

         
         
         //LearningItemBookmarkM itemBookmark = null;
//         if (item!=null){
//
//             String username = request.getRemoteUser();
//             // itemBookmark = lmsViewService.getItemBookmark(item.getItemCode(),username);
//         }
         
//      String code=  request.getParameter("path");
//      int codeInt = Integer.parseInt(code);
//      LearningItemM item = lmsServiceFactory.getLearningItemService().getFullLearningItemByCode(codeInt);
      if (item!=null){
          request.setAttribute(ITEM_ATTR,item);
         // request.setAttribute(ITEM_BOOKMARK_ATTR, itemBookmark);
          switch(item.getItemType()){
              case LMSConstant.ItemTypeConstant.ITEM_COURSE_TYPE:
                  forwardPage="ecourse";
                  break;
              case LMSConstant.ItemTypeConstant.ITEM_LAB_TYPE:
                  forwardPage="elab";
                  break;
              case LMSConstant.ItemTypeConstant.ITEM_TEST_TYPE:
                  forwardPage="etest";
                  break;
              case LMSConstant.ItemTypeConstant.ITEM_ENTRANCE_TYPE:
                  forwardPage="eentrance";
                  break;
              case LMSConstant.ItemTypeConstant.FLASH_TYPE:
                  forwardPage="eflash";
                  break;
              case LMSConstant.ItemTypeConstant.IMAGE_JPEG_TYPE:
                  forwardPage="imageJpeg";
                  break;
              case LMSConstant.ItemTypeConstant.PLAIN_TEXT_TYPE:
                  forwardPage="plainText";
                  break;
              case LMSConstant.ItemTypeConstant.HTML_DOC_TYPE:
                  forwardPage="forwardHTML";
                  break;
              case LMSConstant.ItemTypeConstant.VIDEO_MP3_TYPE:
                  //forwardPage="eVideoMP3";
//                  request.setAttribute(ITEM_ITEMPATH,item.getItemPath());
//                  request.setAttribute(ITEM_FILENAME,item.getFilename());
                  forwardPage ="previewEVideo";
                  break;
              case LMSConstant.ItemTypeConstant.FLASH_VIDEO_TYPE:
                  //forwardPage="eVideoMP3";
                  forwardPage ="flashVideo";
                  break;
          }
//          if (item.getItemType()==LearningItemViewM.ECOURSE_TYPE){
//              forwardPage="ecourse";
//          };
//          if (item.getItemType()==LearningItemViewM.ELAB_TYPE){
//              forwardPage="elab";
//          };
//          if (item.getItemType()==LearningItemViewM.ETEST_TYPE){
//              forwardPage="etest";
//          };
//          if (item.getItemType()==LearningItemViewM.E_ENTRANS_TYPE){
//              forwardPage="eentrance";
//          };
//          if (item.getItemType()==LMSConstant.ItemTypeConstant.IMAGE_JPEG_TYPE){
//              forwardPage = "imageJpeg";
//          }

      }
      return mapping.findForward(forwardPage)  ;
    }

    /**
     * @return the lmsViewService
     */
    public LmsViewService getLmsViewService() {
        return lmsViewService;
    }

    /**
     * @param lmsViewService the lmsViewService to set
     */
    public void setLmsViewService(LmsViewService lmsViewService) {
        this.lmsViewService = lmsViewService;
    }

    public LearningItemHistoryService getHistoryService() {
        return historyService;
    }

    public void setHistoryService(LearningItemHistoryService historyService) {
        this.historyService = historyService;
    }

    /**
     * @return the openIdService
     */
    public OpenIdService getOpenIdService() {
        return openIdService;
    }

    /**
     * @param openIdService the openIdService to set
     */
    public void setOpenIdService(OpenIdService openIdService) {
        this.openIdService = openIdService;
    }

}
